#include <filesystem>


#include <QCoreApplication>
#include <QCommandLineParser>
#include "sslgen.h"
#include "rdpserver.h"
// 全局变量
bool test_dump_rfx_realtime = true;
const char* test_pcap_file = nullptr;

int main(int argc, char* argv[])
{
    QCoreApplication application(argc, argv);
    int port;
    bool localOnly = false;
    // 创建命令行参数解析器
    QCommandLineParser parser;
    parser.setApplicationDescription("RDP Server Application");
    parser.addHelpOption();
    parser.addVersionOption();

    // 添加选项
    parser.addOption(QCommandLineOption("fast", "Disable real-time dump"));
    parser.addOption(QCommandLineOption("port", "Set the port number", "port","3389"));
    parser.addOption(QCommandLineOption("local-only", "Enable local only mode"));
    parser.addOption(QCommandLineOption("pcap", "Set the pcap file", "pcap"));
    parser.addOption(QCommandLineOption("certificate", "The TLS certificate file to use", "certificate","server.crt"));
    parser.addOption(QCommandLineOption("certificate-key", "The TLS certificate key to use", "certificate-key","server.key"));
    // 解析命令行参数
    parser.process(application);

    // 处理选项
    if (parser.isSet("fast")) {
        test_dump_rfx_realtime = false;
    }
    port = parser.value("port").toInt();
    if (parser.isSet("local-only")) {
        localOnly = true;
    }
    if (parser.isSet("pcap")) {
        test_pcap_file = parser.value("pcap").toStdString().c_str();
    }

    auto certificate = std::filesystem::path(parser.value("certificate").toStdString());
    auto certificateKey = std::filesystem::path(parser.value("certificate-key").toStdString());
    bool certificateGenerated = false;
    if (!std::filesystem::exists(certificate) || !std::filesystem::exists(certificateKey)) {
        certificate = "/tmp/server.crt";
        certificateKey = "/tmp/server.key";
        if(ssl_genetate(certificate,certificateKey))
        {
            certificateGenerated = true;
        }
    }
    RdpServer server;
    server.setPort(port);
    server.setLocalOnly(localOnly);
    server.setTlsCertificate(certificate);
    server.setTlsCertificateKey(certificateKey);

    server.start();



	

    auto result = application.exec();
    return result;
}